1 00:00:00,570 --> 00:00:02,970 Welcome back in this lecture. 2 00:00:02,970 --> 00:00:08,580 We're finally getting into the meat and potatoes of our game, which is scripting the system responsible 3 00:00:08,580 --> 00:00:10,260 for handling waves. 4 00:00:10,260 --> 00:00:15,570 This script will start the game once a player spawns into the map and begins a countdown for the first 5 00:00:15,570 --> 00:00:16,590 wave to begin. 6 00:00:16,590 --> 00:00:21,390 Then the script will spawn the zombies for the first wave on the map for the players to kill. 7 00:00:21,390 --> 00:00:26,400 Once all the zombies are dead, it will move into an intermission state before starting the next round, 8 00:00:26,400 --> 00:00:32,280 and it will continue this loop until all the players beat all of the waves or all the players die in 9 00:00:32,280 --> 00:00:32,940 our game. 10 00:00:32,940 --> 00:00:37,950 Either way, we'll have the script, clean up the game, and then wait for a new player to spawn in 11 00:00:37,950 --> 00:00:40,380 to start this loop all over again. 12 00:00:40,840 --> 00:00:47,560 So my plan is to have a total of 11 waves, with the final wave featuring the strongest zombie boss. 13 00:00:47,560 --> 00:00:54,310 So let's go back to our game handler script, which is going to be responsible for this wave system. 14 00:00:54,310 --> 00:01:01,000 I want to define a table and I'm going to call this table my zombie count per wave. 15 00:01:01,000 --> 00:01:06,640 And this is going to tell me how many zombies I should spawn and what type for each wave in my game. 16 00:01:07,120 --> 00:01:14,590 For example, for the first wave in my game, I want to have a set of regular zombies, and I'm going 17 00:01:14,590 --> 00:01:18,130 to set the amount of regular zombies to a value of like 12. 18 00:01:18,130 --> 00:01:23,710 So that means at each one of our spawns, there will be a total of three zombies coming out of them. 19 00:01:23,710 --> 00:01:25,210 That's for the first wave. 20 00:01:25,900 --> 00:01:31,420 And then we can continue on and set how many zombies we would like for the next sequential waves. 21 00:01:31,420 --> 00:01:35,950 For example, for the second wave, maybe we can bump up this value to 20. 22 00:01:36,910 --> 00:01:43,030 For the third wave, we can continue to add more zombies, but this time I'm going to decrease the amount 23 00:01:43,030 --> 00:01:48,820 of regular zombies, and instead we're going to introduce a new zombie, which is our fast zombie. 24 00:01:49,030 --> 00:01:55,570 So let's go ahead and make sure that the keys that we're placing inside of this table match the names 25 00:01:55,570 --> 00:01:57,610 of our zombies in server storage. 26 00:01:57,610 --> 00:02:03,280 So if we want to add some fast zombies, let's go ahead and create a key for a fast zombie. 27 00:02:03,900 --> 00:02:06,120 And that's going to be a total of. 28 00:02:06,120 --> 00:02:10,530 We could do something like eight fast zombies, and that's going to be for wave number three. 29 00:02:10,530 --> 00:02:14,100 And then again we can copy this and do something for wave four. 30 00:02:14,100 --> 00:02:16,680 We'll bump up this value to something like 24. 31 00:02:16,680 --> 00:02:20,670 And then we'll add an additional fast zombies. 32 00:02:21,210 --> 00:02:25,860 And then for the fifth wave again, we can set this to 24. 33 00:02:26,670 --> 00:02:33,990 We'll bump up this value to 12, and then we can introduce another new enemy like our tough zombie and 34 00:02:33,990 --> 00:02:36,150 set that value to something like four. 35 00:02:36,180 --> 00:02:40,080 And then we can copy this and move on to the next wave, which is wave six. 36 00:02:40,080 --> 00:02:42,690 Here we'll bump up the amount of enemies even more. 37 00:02:42,690 --> 00:02:44,670 We'll have this be at 28. 38 00:02:44,700 --> 00:02:48,420 The amount of fast zombies will have will be 16. 39 00:02:48,420 --> 00:02:50,970 We'll increase this to eight. 40 00:02:51,390 --> 00:02:53,610 And then for wave number seven. 41 00:02:54,710 --> 00:02:56,720 We'll keep this value at 28. 42 00:02:56,720 --> 00:02:59,120 We'll keep this one at 16. 43 00:02:59,300 --> 00:03:02,390 We're going to increase the amount of tough zombies to ten. 44 00:03:02,390 --> 00:03:08,390 And then we can introduce another new enemy, which is going to be our fast zombie boss. 45 00:03:08,390 --> 00:03:11,480 And then we can spawn two of those guys onto our map. 46 00:03:11,840 --> 00:03:16,310 Again, we'll copy this and do it for our eighth wave. 47 00:03:16,310 --> 00:03:22,790 For the eighth wave, we could do something like bump this up to 34 for the fast zombies. 48 00:03:22,790 --> 00:03:25,730 We'll bump this up to 20 for the tough zombies. 49 00:03:25,730 --> 00:03:30,620 We can bump this up to 12, and then we'll double the amount of fast zombie bosses. 50 00:03:30,620 --> 00:03:33,470 And then again, we can introduce another new enemy. 51 00:03:33,470 --> 00:03:37,130 And this can be our tough zombie. 52 00:03:37,670 --> 00:03:38,990 Let me make sure I spell this correctly. 53 00:03:38,990 --> 00:03:39,980 Tough zombie boss. 54 00:03:39,980 --> 00:03:46,310 And we'll place two of those guys onto the map and actually move this down so our table doesn't get 55 00:03:46,310 --> 00:03:47,360 too long. 56 00:03:47,360 --> 00:03:51,230 And then we can copy this and move on to the ninth wave. 57 00:03:51,410 --> 00:03:54,800 And for the ninth wave, we can bump up this to 36. 58 00:03:54,800 --> 00:04:00,680 We'll set the amount of fast zombies to 28, something like that. 59 00:04:00,680 --> 00:04:02,930 We'll bump this up to 16. 60 00:04:02,930 --> 00:04:08,270 We can bump this up to, let's say, five of these guys. 61 00:04:08,270 --> 00:04:13,610 We'll bump this up to four of these guys, and then we'll introduce another new enemy. 62 00:04:13,610 --> 00:04:16,580 And that's going to be our lightning zombie. 63 00:04:18,200 --> 00:04:22,490 And we can spawn something like, let's say, eight of those guys onto the map. 64 00:04:25,280 --> 00:04:29,990 And then again, we can copy this and do it again for our 10th wave. 65 00:04:31,560 --> 00:04:33,720 We could bump this value up to 40. 66 00:04:33,750 --> 00:04:35,850 We could keep this value the same. 67 00:04:35,850 --> 00:04:40,800 We'll bump up the amount of zombies equal to, let's say, 18. 68 00:04:40,800 --> 00:04:46,320 We'll bump up the amount of fast zombie bosses to, let's say eight. 69 00:04:46,870 --> 00:04:51,880 And then the tough zombie bosses will bump up to five, and then the lightning zombies will bump up 70 00:04:51,880 --> 00:04:53,170 to 12. 71 00:04:53,880 --> 00:04:55,680 And then we'll copy this once more. 72 00:04:55,680 --> 00:04:58,560 And for our final wave, which is wave 11. 73 00:04:59,310 --> 00:05:01,710 We'll bump up this to 44. 74 00:05:01,740 --> 00:05:03,990 We'll bump this up to 30. 75 00:05:04,020 --> 00:05:07,020 We'll bump this one up to 20. 76 00:05:07,320 --> 00:05:12,060 We'll keep this one the same, but we'll add an additional tough zombie boss. 77 00:05:12,060 --> 00:05:15,870 We'll bump up this value to, let's say 14. 78 00:05:15,870 --> 00:05:23,010 And then we're going to add our final zombie, which is our ultimate zombie boss. 79 00:05:23,580 --> 00:05:26,640 And we're going to add one of those guys into our map. 80 00:05:27,500 --> 00:05:32,360 Now you can totally have the freedom to change these values however you'd like to balance out the game 81 00:05:32,360 --> 00:05:37,010 in whatever way you see fit, but I'm just going to keep it at these values for now. 82 00:05:38,710 --> 00:05:43,420 Next thing I want to go ahead and do is I want to keep track of the total waves that we're going to 83 00:05:43,420 --> 00:05:44,140 have in our game. 84 00:05:44,140 --> 00:05:49,090 So let's go ahead and make a few constants one to signify the total amount of waves in our game, which 85 00:05:49,090 --> 00:05:49,960 is 11. 86 00:05:49,960 --> 00:05:55,060 We also want to define how long we should wait in the intermission stage between waves. 87 00:05:55,060 --> 00:05:58,690 So we'll call this our wait time for the intermission. 88 00:05:58,690 --> 00:06:00,850 And we could do something like 60s. 89 00:06:00,850 --> 00:06:05,710 And then we can also define the wait time when we are in the starting intermission. 90 00:06:05,710 --> 00:06:12,040 So we'll call this wait time intermission or underscore start underscore intermission and we'll do something 91 00:06:12,040 --> 00:06:13,240 like 30s. 92 00:06:14,670 --> 00:06:20,190 Then we can go ahead and define a bunch of functions for what we want to do in each one of the different 93 00:06:20,190 --> 00:06:21,960 states that our game can be in. 94 00:06:21,960 --> 00:06:25,530 For example, what do we want to do when we're in the starting intermission? 95 00:06:25,530 --> 00:06:28,530 So we can call this function on start intermission? 96 00:06:28,830 --> 00:06:32,070 What do we want to do when we're in the regular intermission? 97 00:06:32,070 --> 00:06:34,380 We'll call this function on intermission. 98 00:06:34,650 --> 00:06:38,970 What do we want to do when we are in the waves in progress state. 99 00:06:38,970 --> 00:06:42,510 So we'll call this function on start wave. 100 00:06:42,930 --> 00:06:46,890 What do we want to do when the game is over and all the players lost? 101 00:06:46,890 --> 00:06:49,140 We'll call this function on game Over. 102 00:06:49,950 --> 00:06:55,290 And then we could copy this and do the exact same thing, except for when the players have won. 103 00:06:55,290 --> 00:06:58,050 So we could call this on game over win. 104 00:06:58,980 --> 00:07:03,330 And then what do we want to do when we're cleaning up the game and we're in that waiting intermission 105 00:07:03,330 --> 00:07:03,630 state? 106 00:07:03,630 --> 00:07:06,090 So we call this function on waiting. 107 00:07:07,760 --> 00:07:13,610 So when we go down here and we set the workspace as game state to the start and intermission, this 108 00:07:13,610 --> 00:07:16,760 is basically going to set our loop into action. 109 00:07:16,760 --> 00:07:22,850 So that means we need to listen for when the workspace has the game state attribute changed. 110 00:07:22,850 --> 00:07:29,270 And then when the attribute changes we want to check what exactly the current game state is, and then 111 00:07:29,270 --> 00:07:32,720 call the associated function based on that game state. 112 00:07:32,720 --> 00:07:38,600 So what we can go ahead and do is we can listen for when the workspace get attribute change signal. 113 00:07:39,170 --> 00:07:43,220 And we're going to be listening to the game state we want to listen for when that is changed. 114 00:07:44,980 --> 00:07:53,650 And then we can go ahead and basically place all of these functions inside of a table and then associate 115 00:07:53,650 --> 00:07:55,600 these functions with a particular key. 116 00:07:55,720 --> 00:07:58,120 So this is going to act as a switch statement. 117 00:07:58,120 --> 00:08:02,620 If you've ever heard of those in other programming languages, which basically allows you to execute 118 00:08:02,620 --> 00:08:05,680 a block of code based on an input. 119 00:08:05,680 --> 00:08:11,380 Unfortunately Lua doesn't have switch statements, but we can use tables to imitate a switch statement 120 00:08:11,380 --> 00:08:12,670 by using a dictionary. 121 00:08:12,670 --> 00:08:16,630 So I'm going to create a table called Intermission functions. 122 00:08:18,380 --> 00:08:23,600 And we're going to associate each one of these states that our game can be in with one of these functions 123 00:08:23,600 --> 00:08:24,260 up here. 124 00:08:24,260 --> 00:08:30,710 So for example, one of the keys could be for our game state enum of start intermission. 125 00:08:30,710 --> 00:08:32,600 What function will be associated with that. 126 00:08:32,600 --> 00:08:36,500 Well that would be associated with our on start intermission function. 127 00:08:36,500 --> 00:08:37,910 And we don't want to call it here. 128 00:08:37,910 --> 00:08:41,840 Instead we want to associate this function with this key. 129 00:08:41,840 --> 00:08:48,410 That way when the game state changes, we can check what the current game state is, grab the associated 130 00:08:48,410 --> 00:08:51,320 function with that game state and then execute that function. 131 00:08:52,050 --> 00:08:55,740 The next would be our game state enum of intermission. 132 00:08:55,740 --> 00:08:58,530 We'll associate that one with the en intermission function. 133 00:08:59,420 --> 00:09:05,720 And then we'll copy this and then do the same thing, but this time for the wave in progress and associate 134 00:09:05,720 --> 00:09:07,940 that with the on start wave function. 135 00:09:10,190 --> 00:09:10,580 Again. 136 00:09:10,580 --> 00:09:16,970 We could do this for the game over game state, and we'll associate that with the on game over function. 137 00:09:18,140 --> 00:09:24,470 Copy this and then listen to the game over win and associate that with the on game over win function. 138 00:09:25,310 --> 00:09:28,850 And then the last state is going to be our waiting state. 139 00:09:28,850 --> 00:09:32,810 And we'll associate that with the on waiting function. 140 00:09:32,810 --> 00:09:37,970 So now what we can go ahead and do is we can get the current game state which is equal to workspace 141 00:09:37,970 --> 00:09:39,650 get attribute game state. 142 00:09:41,270 --> 00:09:47,270 And then we could compare this value with the values inside of our intermission functions table and 143 00:09:47,270 --> 00:09:50,060 get that particular callback function and execute it. 144 00:09:50,150 --> 00:09:51,380 So we'll make a variable. 145 00:09:51,380 --> 00:09:52,520 We'll call it callback. 146 00:09:52,520 --> 00:09:54,800 And that's going to be equal to our intermission functions. 147 00:09:54,800 --> 00:09:57,320 And then we're going to pass the current game state. 148 00:09:59,080 --> 00:10:02,260 And if we have a callback inside of this table. 149 00:10:03,050 --> 00:10:08,690 So if we got a value stored inside of this variable, then we can go ahead and call that function. 150 00:10:10,110 --> 00:10:15,030 So just as a placeholder, what I'm going to do here is I'm going to print. 151 00:10:16,220 --> 00:10:19,640 Starting intermission. 152 00:10:20,660 --> 00:10:22,370 I'll print here. 153 00:10:22,370 --> 00:10:23,840 Intermission. 154 00:10:25,530 --> 00:10:29,340 And in here I'll print wave in progress. 155 00:10:31,510 --> 00:10:32,740 I'll print in this one. 156 00:10:32,740 --> 00:10:34,570 And game over. 157 00:10:34,750 --> 00:10:36,280 Lose. 158 00:10:37,170 --> 00:10:37,530 Here. 159 00:10:37,530 --> 00:10:39,780 I'll print Game Over win. 160 00:10:39,780 --> 00:10:42,840 And then here I will print cleanup. 161 00:10:43,020 --> 00:10:48,780 And the purpose of each of these functions is after they're done executing their specific functionality, 162 00:10:48,780 --> 00:10:55,740 they're going to set the attribute on the workspace of game state to the next, um, game state. 163 00:10:55,740 --> 00:11:00,060 For example, if we're in the wave in progress and then the wave is over, it's going to set it back 164 00:11:00,060 --> 00:11:04,710 to the intermission, and then the intermission is going to wait and then set it back to the wave in 165 00:11:04,710 --> 00:11:05,100 progress. 166 00:11:05,100 --> 00:11:08,730 And it's going to continue that loop until we either lose or we win. 167 00:11:09,610 --> 00:11:14,770 So to demonstrate this loop, what I'm going to do is in the on start intermission function, I'm just 168 00:11:14,770 --> 00:11:16,660 going to wait for five seconds. 169 00:11:16,660 --> 00:11:23,140 And then on the workspace, I'm going to set the attribute of game state equal to the wave in progress 170 00:11:23,140 --> 00:11:23,590 game state. 171 00:11:23,590 --> 00:11:31,180 So we're going to do game state enum dot wave in progress which is going to trigger this function. 172 00:11:31,180 --> 00:11:33,400 So this is going to print out wave in progress. 173 00:11:33,400 --> 00:11:35,770 And then we're going to yield for five seconds. 174 00:11:35,770 --> 00:11:42,790 And then we're going to set the workspaces game state to the intermission which is going to execute 175 00:11:42,790 --> 00:11:43,840 this function. 176 00:11:43,840 --> 00:11:46,420 And then we'll yield in here for five seconds. 177 00:11:47,420 --> 00:11:52,760 And then we'll set the attribute to the. 178 00:11:53,330 --> 00:11:58,940 Game state enum of wave in progress, and then that will continue that loop. 179 00:11:58,940 --> 00:12:03,290 And just to keep track of it, I'll create a temporary variable out here, call it I. 180 00:12:03,680 --> 00:12:08,000 And each time through this loop we're going to increment I by one. 181 00:12:08,480 --> 00:12:11,510 So we'll do I plus equal one. 182 00:12:11,510 --> 00:12:15,020 And then if I goes beyond let's say three. 183 00:12:15,730 --> 00:12:17,860 Then what we can go ahead and do. 184 00:12:18,610 --> 00:12:20,740 Is will set the. 185 00:12:22,470 --> 00:12:27,000 Attribute to the game over win, so the game is over. 186 00:12:27,420 --> 00:12:32,670 Otherwise, we'll set the attribute to another wave that is in progress. 187 00:12:33,660 --> 00:12:40,050 And that should trigger this function right here, which is going to basically display to all the players. 188 00:12:40,050 --> 00:12:40,680 Hey, you won. 189 00:12:40,680 --> 00:12:42,600 And then it'll trigger the cleanup. 190 00:12:42,600 --> 00:12:45,150 So we'll wait here for five seconds. 191 00:12:45,330 --> 00:12:51,210 And then we'll set the attribute on the workspace to the waiting attribute. 192 00:12:52,060 --> 00:12:54,310 And that's going to trigger this function. 193 00:12:54,310 --> 00:13:00,970 And inside of here, we'll wait for five seconds and then set the attribute back to the starting intermission, 194 00:13:00,970 --> 00:13:05,200 which is going to force the loop to start all over again. 195 00:13:05,200 --> 00:13:13,450 And then I'm also going to make sure to set I back to one when we set this to the game over win attribute. 196 00:13:13,450 --> 00:13:19,240 Okay, so this should demonstrate the entire loop of our wave system. 197 00:13:19,540 --> 00:13:26,410 So if we go and just run our game, we should see printed in the console that we are in the starting 198 00:13:26,410 --> 00:13:27,400 intermission. 199 00:13:27,400 --> 00:13:31,510 And then we should see that we are in a wave in progress. 200 00:13:31,510 --> 00:13:36,580 And then when that wave is over, we should get placed in the intermission when the intermission is 201 00:13:36,580 --> 00:13:37,300 over. 202 00:13:38,260 --> 00:13:40,390 We should get placed back into a wave in progress. 203 00:13:40,390 --> 00:13:43,810 So it's going to do that for three waves in total. 204 00:13:43,810 --> 00:13:45,670 So we're back in an intermission. 205 00:13:47,760 --> 00:13:50,700 And then we're back in a wave and this is our third wave. 206 00:13:50,700 --> 00:13:56,220 And then we should go to the game over when we're back into an intermission. 207 00:13:56,220 --> 00:13:58,020 So maybe it'll do four waves. 208 00:14:02,160 --> 00:14:04,290 Game over, when actually, that's kind of weird. 209 00:14:04,820 --> 00:14:08,150 So we're in a game over win and then we're in the cleanup. 210 00:14:09,170 --> 00:14:13,460 And now we're back in the starting intermission, and it's going to do that entire loop all over again. 211 00:14:13,460 --> 00:14:15,170 So this is our game loop. 212 00:14:16,770 --> 00:14:20,190 I'm actually kind of interested to see why it set it to the intermission. 213 00:14:20,400 --> 00:14:21,660 So. 214 00:14:22,850 --> 00:14:24,020 We go back. 215 00:14:24,860 --> 00:14:25,940 When we increment I. 216 00:14:25,940 --> 00:14:27,830 If I is greater than three. 217 00:14:27,830 --> 00:14:31,970 Oh we could we should have done, uh, greater than or equal to three. 218 00:14:31,970 --> 00:14:33,350 But you get what I mean. 219 00:14:33,980 --> 00:14:35,450 It'll go for three waves. 220 00:14:35,450 --> 00:14:38,960 And then once the three waves are over our players, one, it triggers the cleanup. 221 00:14:38,960 --> 00:14:41,720 And then the cleanup puts us back in the start intermission. 222 00:14:41,720 --> 00:14:46,700 And then it starts the next wave, and it just continues that loop forever and ever and ever. 223 00:14:46,700 --> 00:14:51,470 So this is the kind of system we want to implement for spawning zombies into our game. 224 00:14:52,610 --> 00:14:57,680 So for example, when we get set to the start intermission, what we want to do is have a countdown 225 00:14:57,680 --> 00:15:03,530 here of 30s to let all the players know that, hey, we're going to be starting, uh, the first wave 226 00:15:03,530 --> 00:15:05,030 and a certain amount of seconds. 227 00:15:05,030 --> 00:15:12,260 So this means our if I go and enable UI visibility, this guy up here is going to have to come into 228 00:15:12,260 --> 00:15:14,240 play for our clients. 229 00:15:14,240 --> 00:15:16,070 That is our game state guy. 230 00:15:16,070 --> 00:15:17,690 It's simply a text label. 231 00:15:17,690 --> 00:15:23,390 And we're going to update the text in this text label to communicate to the player, like what wave 232 00:15:23,390 --> 00:15:27,920 we are in, how many zombies are left alive if we're in the cleanup stage, stuff like that. 233 00:15:27,920 --> 00:15:34,520 So we have an event inside of replicated storage to communicate between the client and the server, 234 00:15:34,520 --> 00:15:37,190 to update that text label for that player. 235 00:15:37,580 --> 00:15:40,250 So we can go ahead and make a reference to that event. 236 00:15:40,250 --> 00:15:41,960 I'm going to call it my game event. 237 00:15:41,960 --> 00:15:44,180 And that's in replicated storage dot remotes. 238 00:15:44,180 --> 00:15:46,190 And it's called Game event. 239 00:15:46,840 --> 00:15:51,040 And then we want to be able to create a standard of communication between the client and the server 240 00:15:51,040 --> 00:15:51,820 with this event. 241 00:15:51,820 --> 00:15:56,560 So let's go ahead and create a new module script inside of replicated storage. 242 00:15:56,560 --> 00:15:59,710 I'll just duplicate this game state enum that we already have. 243 00:16:00,100 --> 00:16:04,600 And inside of here we'll have a section for actions to be given to the client. 244 00:16:04,600 --> 00:16:06,430 So we'll call this two client. 245 00:16:06,910 --> 00:16:11,830 And one of the actions we want to give to the client is for them to update the text on their screen. 246 00:16:11,830 --> 00:16:15,370 So we'll call it update text and set equal to itself. 247 00:16:16,090 --> 00:16:19,900 And then we'll rename this to our game Comms enum. 248 00:16:20,900 --> 00:16:22,850 Now we can go ahead and use this enum. 249 00:16:22,850 --> 00:16:26,360 So we'll require it Game.com enum. 250 00:16:26,360 --> 00:16:31,760 And we'll require for replicated storage dot modules dot enums dot game comms enum. 251 00:16:32,920 --> 00:16:37,750 And then now that we have this event and this enum inside of our starting intermission, what we want 252 00:16:37,750 --> 00:16:44,140 to go ahead and do is when we're starting the intermission, we'll use the game event and fire to all 253 00:16:44,140 --> 00:16:45,430 of the clients in our game. 254 00:16:45,430 --> 00:16:51,760 Hey, you need to update that text on your screen, so we'll tell them to update the text and the text 255 00:16:51,760 --> 00:16:53,620 will update it to is. 256 00:16:53,620 --> 00:16:58,210 We want to tell them that we're waiting for players to join the game, because we're in the starting 257 00:16:58,210 --> 00:16:59,110 intermission. 258 00:16:59,990 --> 00:17:05,540 And then what we're going to do is since there's not going to be any players spawned onto the map, 259 00:17:05,540 --> 00:17:11,000 we need to wait for when a player is added to our live team, and that will notify us when a player 260 00:17:11,000 --> 00:17:17,480 has pressed the play button and spawned into the map, which means we can start the countdown for the 261 00:17:17,480 --> 00:17:18,500 start intermission. 262 00:17:18,770 --> 00:17:25,130 So inside of the team service, we're going to wait for the player added event to be fired for our live 263 00:17:25,160 --> 00:17:25,700 team. 264 00:17:26,520 --> 00:17:31,710 Once that gets fired, that means a player has spawned into the map and we can go ahead and start our 265 00:17:31,710 --> 00:17:32,160 countdown. 266 00:17:32,160 --> 00:17:33,810 So we're going to create a while loop. 267 00:17:33,810 --> 00:17:39,120 The starting index for I is going to be equal to our wait time for the start intermission. 268 00:17:39,120 --> 00:17:41,820 And then we're going to go all the way until we hit one second. 269 00:17:41,820 --> 00:17:44,490 And we're going to subtract by one each time. 270 00:17:45,810 --> 00:17:51,300 And what we're going to do here is we're going to copy this event, but this time we're going to set 271 00:17:51,300 --> 00:17:55,500 the text to be waves our starting in. 272 00:17:55,710 --> 00:17:59,700 And then we can go ahead and concatenate this with the current index that we're at. 273 00:17:59,880 --> 00:18:02,130 And we'll say seconds. 274 00:18:02,130 --> 00:18:06,300 And then we'll make sure to yield for one second each time through this for loop. 275 00:18:07,740 --> 00:18:15,570 Now, something very important to note is I want the game over, lose game state to execute when there 276 00:18:15,570 --> 00:18:19,590 are no longer any players left on the alive team. 277 00:18:19,620 --> 00:18:26,010 Now that means if a player joins the game and they get put on the alive team, but then they decided 278 00:18:26,010 --> 00:18:33,990 to troll and then reset right away, well, that means it's going to trigger this state, and that means 279 00:18:33,990 --> 00:18:38,640 we don't want to continue through this loop for our countdown. 280 00:18:38,640 --> 00:18:43,860 So another thing we should check inside of this countdown is if the game state is still equal to the 281 00:18:43,860 --> 00:18:45,060 start intermission. 282 00:18:45,060 --> 00:18:54,120 If the workspace getattribute game state if it is not equal to the game state enum of start intermission, 283 00:18:54,120 --> 00:18:59,730 then that means we know that a player or all the players in our map have died, so we should go ahead 284 00:18:59,730 --> 00:19:02,880 and just return out of this function and do nothing else. 285 00:19:03,930 --> 00:19:10,020 Otherwise, once we reach the end of this for loop, we can go ahead and set the game state to the game 286 00:19:10,020 --> 00:19:11,730 state of wave and progress. 287 00:19:11,730 --> 00:19:19,260 And just as an extra set or layer of security, we're also going to put this if statement on the outside 288 00:19:19,260 --> 00:19:22,380 of our for loop, just to make sure we're still in the start intermission. 289 00:19:22,380 --> 00:19:27,540 And if we're still in the start intermission, then it's okay for us to set the game state to waves 290 00:19:27,540 --> 00:19:28,620 in progress. 291 00:19:29,620 --> 00:19:33,910 Now, in order for the clients to see this text appear on their screen, we need to go ahead and create 292 00:19:33,910 --> 00:19:36,880 a new local script to listen for this game event. 293 00:19:36,880 --> 00:19:44,500 So let's create a new local script inside of our game state guy and we'll call this our game state Guy 294 00:19:44,530 --> 00:19:45,220 handler. 295 00:19:46,480 --> 00:19:52,420 And inside of here, what we need to do is get replicated storage to refer to our game event as well 296 00:19:52,420 --> 00:19:53,950 as our game comms enum. 297 00:19:53,950 --> 00:19:56,770 So we'll go ahead and grab replicated storage. 298 00:19:58,780 --> 00:20:02,230 And then we can go ahead and get our game comms enum. 299 00:20:03,600 --> 00:20:09,600 And will require from replicated storage modules, enums, game comms, enum and then will make a reference 300 00:20:09,600 --> 00:20:10,800 to our game event. 301 00:20:10,980 --> 00:20:13,980 So that's then remotes dot game event. 302 00:20:14,570 --> 00:20:18,020 And then we also make a reference to our GUI which is script dot parent. 303 00:20:18,020 --> 00:20:21,230 And then we'll get the text label that is inside of our GUI. 304 00:20:21,230 --> 00:20:23,270 So that's in GUI dot frame. 305 00:20:23,270 --> 00:20:25,100 And then we'll get the text label. 306 00:20:25,670 --> 00:20:30,140 And now what we can go ahead and do is we can listen for when the game event is fired. 307 00:20:30,140 --> 00:20:32,210 So we'll listen to on client event. 308 00:20:32,210 --> 00:20:34,010 And then we'll connect a function. 309 00:20:34,010 --> 00:20:39,920 And the server is going to give us an action as well as some text to update on our screen. 310 00:20:40,010 --> 00:20:47,930 So if this action is equal to our game comms enum of two client dot update text, then we can go ahead 311 00:20:47,930 --> 00:20:52,700 and set the text label's text property equal to this text from the server. 312 00:20:52,700 --> 00:20:56,210 And that's all we need to do for our game state UI handler. 313 00:20:57,160 --> 00:21:02,290 Now we can go ahead and test what we've gotten written down so far, and we should see a countdown appear 314 00:21:02,290 --> 00:21:06,430 on our screen when we are set to the start intermission. 315 00:21:06,430 --> 00:21:09,160 So if we go and play test the game. 316 00:21:12,370 --> 00:21:14,590 And we go and play the game. 317 00:21:15,340 --> 00:21:21,400 As you can see it now, says waves are starting in however many seconds, and as one second goes by, 318 00:21:21,400 --> 00:21:27,790 it continues to decrement this value and then eventually it should go to 1 or 0, and then it should 319 00:21:27,790 --> 00:21:29,680 start the first wave. 320 00:21:29,680 --> 00:21:35,770 So we'll go ahead and pay attention inside of our console if it prints that message of wave in progress. 321 00:21:37,250 --> 00:21:42,080 So 54321. 322 00:21:42,080 --> 00:21:43,790 And then there we go. 323 00:21:43,820 --> 00:21:45,290 Wave in progress. 324 00:21:45,290 --> 00:21:50,600 And this is where we should update the text on the player's screen again and tell them that, hey, 325 00:21:50,600 --> 00:21:53,090 a wave is in progress, stuff like that. 326 00:21:53,090 --> 00:21:57,950 And you can also see that our shop is opening and closing. 327 00:21:57,950 --> 00:21:59,780 See, we're in the intermission state. 328 00:21:59,780 --> 00:22:08,330 So the shop opened, but then when we are in Game Over or we were in a wave, then the shop closes down 329 00:22:08,330 --> 00:22:10,850 and we can't interact with the shop anymore. 330 00:22:12,450 --> 00:22:16,740 But then once we're back in the starting intermission, as you can see, we can now interact with the 331 00:22:16,740 --> 00:22:18,210 shot, which is very cool. 332 00:22:19,530 --> 00:22:24,390 Now, what do we want to do when the game state is set to waves in progress? 333 00:22:24,420 --> 00:22:28,080 Well, it's going to trigger our on start wave function. 334 00:22:28,080 --> 00:22:35,520 And what this should do is tell all the players that a wave is starting, and then we should basically 335 00:22:35,520 --> 00:22:39,600 display on the screen that a wave is in progress, how many zombies are left. 336 00:22:39,600 --> 00:22:44,460 And then we should spawn all of these zombies onto the map for our players to fight. 337 00:22:44,850 --> 00:22:50,850 So what we're going to do is we're going to get rid of this yield statement here. 338 00:22:51,490 --> 00:22:55,930 And we want to keep track of what wave we're currently on. 339 00:22:55,930 --> 00:22:59,380 So we're going to replace this eye variable here. 340 00:22:59,380 --> 00:23:01,930 We're just going to get rid of all of this for now. 341 00:23:03,000 --> 00:23:06,720 And we'll create a new variable up here that's going to keep track of the current wave. 342 00:23:06,720 --> 00:23:08,730 And we'll just call it current wave. 343 00:23:09,990 --> 00:23:15,450 And we're going to set that equal to one by default or actually no, we'll set it to zero by default. 344 00:23:16,360 --> 00:23:17,890 And then we'll go down. 345 00:23:17,890 --> 00:23:22,390 And when we start a wave, we'll set current wave plus equal one. 346 00:23:22,930 --> 00:23:26,980 And then we want to get a table of all these zombies we need to spawn on the map. 347 00:23:26,980 --> 00:23:29,380 So we'll call this zombies to spawn. 348 00:23:29,770 --> 00:23:37,060 And what we should do is we should loop through all of the zombies that are currently in whatever table 349 00:23:37,060 --> 00:23:38,110 for what wave we're in. 350 00:23:38,110 --> 00:23:42,700 So if we're in wave number one, we're going to loop through all of the key value pairs and grab the 351 00:23:42,700 --> 00:23:44,950 total amount of zombies we need to spawn. 352 00:23:45,370 --> 00:23:46,930 So what we can do? 353 00:23:47,750 --> 00:23:54,830 Is will loop through every single zombie and the amount in the zombie count per wave. 354 00:23:54,830 --> 00:23:57,950 And we're going to index it with the string of wave. 355 00:23:57,950 --> 00:24:01,850 And then we're going to concatenate that string with whatever current wave we're at. 356 00:24:03,550 --> 00:24:08,860 And what we're going to do is we're going to make a clone of this particular zombie and insert it into 357 00:24:08,860 --> 00:24:10,600 our zombies to spawn table. 358 00:24:10,600 --> 00:24:17,710 So for I equal to one with the amount of zombies, what we need to do is make a clone of this zombie. 359 00:24:17,710 --> 00:24:23,290 So we'll basically need to get a reference to our zombies folder which is in server storage. 360 00:24:23,290 --> 00:24:25,060 So let's grab server storage. 361 00:24:28,340 --> 00:24:34,880 And then we'll have a variable to refer to our zombies folder, and we'll set that equal to server storage 362 00:24:34,880 --> 00:24:36,560 dot zombies. 363 00:24:37,690 --> 00:24:41,860 So now we have a reference to the folder where all the zombies in our game are stored. 364 00:24:42,410 --> 00:24:47,630 And then we can go ahead and refer to our zombies folder and then index it with the particular name 365 00:24:47,630 --> 00:24:49,160 of our zombie, which is right here. 366 00:24:49,160 --> 00:24:51,080 So we'll pass that right there. 367 00:24:51,080 --> 00:24:53,960 And then we're going to make a clone of that zombie. 368 00:24:54,350 --> 00:24:59,390 And then we can go ahead and insert this clone into our zombies to spawn table. 369 00:24:59,390 --> 00:25:05,750 And then once we have all of the zombies we need to spawn inside of our table, we can go ahead and 370 00:25:05,750 --> 00:25:11,000 tell all of the players in our game, hey, a wave is in progress and this is how many zombies are left 371 00:25:11,000 --> 00:25:12,170 to fight. 372 00:25:12,170 --> 00:25:19,340 So we'll fire to all of the clients that wave, and then we'll index this with whatever the current 373 00:25:19,340 --> 00:25:19,880 wave is at. 374 00:25:19,880 --> 00:25:25,490 So wave number one, wave number two, whatever wave whatever is in progress. 375 00:25:26,310 --> 00:25:30,690 And then we'll separate this and say something like how many zombies are left? 376 00:25:30,690 --> 00:25:35,430 So we'll concatenate this with a value to represent how many zombies are left in the game. 377 00:25:35,430 --> 00:25:36,810 So let's make a variable for that. 378 00:25:36,810 --> 00:25:38,700 We'll call this zombies left. 379 00:25:38,700 --> 00:25:42,960 And that's going to be equal to the number of zombies to spawn in our table. 380 00:25:43,320 --> 00:25:45,990 So the number of zombies left. 381 00:25:45,990 --> 00:25:50,550 And then we'll concatenate this with the string of zombies left. 382 00:25:50,550 --> 00:25:55,380 So if it's wave one and we have to fight 12 zombies, it'll say wave one in progress. 383 00:25:55,380 --> 00:25:57,240 12 zombies left. 384 00:25:58,110 --> 00:26:04,680 Now, what we need to go ahead and do is have a loop executing here that's going to loop through and 385 00:26:04,680 --> 00:26:09,420 randomly spawn all the zombies on the map in a circular motion. 386 00:26:09,420 --> 00:26:14,790 So basically we're going to start at one spawn and then move to the next spawn, and then move to the 387 00:26:14,790 --> 00:26:16,230 next spawn and move to the next. 388 00:26:16,230 --> 00:26:21,060 And continue in that loop until there are no more zombies left to spawn. 389 00:26:21,060 --> 00:26:24,810 So a perfect way we could do that is by using a repeat loop. 390 00:26:24,810 --> 00:26:30,510 We're going to repeatedly spawn zombies on the map until we have no more left to spawn. 391 00:26:31,140 --> 00:26:36,660 So what we can go ahead and do is pick a random zombie out of our zombies to spawn table by generating 392 00:26:36,660 --> 00:26:40,080 a random number, we'll call this random num, and that's equal to RNG. 393 00:26:40,080 --> 00:26:41,340 Next integer. 394 00:26:41,960 --> 00:26:44,180 And we actually need to create a random data type. 395 00:26:44,180 --> 00:26:45,530 So let's do that as well. 396 00:26:46,130 --> 00:26:48,860 RNG is equal to random dot new. 397 00:26:49,250 --> 00:26:57,350 So we'll generate a new integer from one to the length of our zombies to spawn table. 398 00:26:59,030 --> 00:27:02,960 And then we can go ahead and get that zombie at this index. 399 00:27:03,230 --> 00:27:08,930 And we want to go ahead and position this zombie at one of the spawns in our map. 400 00:27:08,930 --> 00:27:15,710 For example, if we need to position the zombie over here, we'll set it to this spawn and set the humanoid 401 00:27:15,710 --> 00:27:20,480 root part of that zombie equal to the position of this attachment in our world. 402 00:27:21,240 --> 00:27:27,300 So we need to also create a variable to store all the different zombie spawns that we have in our map. 403 00:27:28,230 --> 00:27:31,710 So up here we'll create another variable. 404 00:27:31,710 --> 00:27:35,490 And I'm going to call this zombie spawns. 405 00:27:35,490 --> 00:27:40,200 And that's going to be equal to workspace dot spawns dot zombie spawns. 406 00:27:40,200 --> 00:27:43,740 And we're just going to get all of those spawns and store it in a table. 407 00:27:44,430 --> 00:27:47,490 And then we can go ahead and refer to this zombie. 408 00:27:47,490 --> 00:27:55,230 We'll get their humanoid root part and set the key frame equal to one of the positions of the attachments 409 00:27:55,230 --> 00:27:56,490 that are in our spawn. 410 00:27:56,490 --> 00:28:03,120 Now, since we only have four spawns and we want to go in a loop starting at one and then going to the 411 00:28:03,120 --> 00:28:07,740 next one, next one, and then going back to one and the next and the next, we need to keep track of 412 00:28:07,740 --> 00:28:13,410 what current index we are inside of the array that is storing all of our zombie spawns. 413 00:28:13,950 --> 00:28:16,800 So let's create a variable outside of our repeat loop. 414 00:28:16,800 --> 00:28:19,770 We'll call it spawn index and set it equal to one. 415 00:28:19,860 --> 00:28:22,590 And then we can refer to our zombie spawns. 416 00:28:23,820 --> 00:28:29,700 Get the spawn at this current spawn index, and then we'll refer to the attachment that is inside of 417 00:28:29,700 --> 00:28:32,640 that part, and then get its world keyframe. 418 00:28:33,210 --> 00:28:36,360 And then we can go ahead and increment our spawn index by one. 419 00:28:36,360 --> 00:28:37,800 So we'll do plus equal one. 420 00:28:38,070 --> 00:28:43,920 And then we'll make sure to check if this spawn index goes beyond the number of spawns that we actually 421 00:28:43,920 --> 00:28:44,640 have in our map. 422 00:28:44,640 --> 00:28:51,750 So if spawn index is greater than the number of zombie spawns we have, then we can just go ahead and 423 00:28:51,750 --> 00:28:53,490 set spawn index back to one. 424 00:28:53,490 --> 00:28:57,000 And this allows us to loop through each one of our zombie spawns. 425 00:28:57,420 --> 00:29:02,970 Once we've successfully positioned our zombie to the correct area on our map, then we can go ahead 426 00:29:02,970 --> 00:29:09,990 and refer to this same zombie and set its parent equal to the folder in our workspace. 427 00:29:09,990 --> 00:29:12,270 So let's create a variable for that as well. 428 00:29:13,010 --> 00:29:18,200 I'll call this zombies workspace and that's equal to workspace dot zombies. 429 00:29:18,560 --> 00:29:24,740 And then we can go ahead and set the parent of this zombie equal to our zombies folder in the workspace. 430 00:29:25,220 --> 00:29:30,650 Now that our zombie is in the workspace, we need to go ahead and listen for when this zombie dies. 431 00:29:30,650 --> 00:29:35,990 Because when this zombie dies, we need to update the text displayed for the players to tell them how 432 00:29:35,990 --> 00:29:37,430 many zombies are left. 433 00:29:37,820 --> 00:29:43,910 So if this particular zombie to spawn at this index, we're going to get their humanoid and we're going 434 00:29:43,910 --> 00:29:45,560 to listen to their died event. 435 00:29:47,650 --> 00:29:52,630 And with this, what we can go ahead and do is we can decrement the zombies left the variable. 436 00:29:52,630 --> 00:29:56,320 So we'll do zombies left minus equal one. 437 00:29:56,920 --> 00:30:03,070 And then we can just basically copy what we've done right here and give this new updated text to our 438 00:30:03,070 --> 00:30:03,970 players. 439 00:30:05,640 --> 00:30:11,730 And now, every single time a zombie is going to die, it's going to decrement it and tell the players 440 00:30:11,730 --> 00:30:13,770 eventually, once there are no zombie left. 441 00:30:13,770 --> 00:30:17,940 And we need to check inside of this event when there are no zombies left. 442 00:30:17,940 --> 00:30:21,900 So if the number of zombies left is equal to zero. 443 00:30:23,150 --> 00:30:29,180 Then what we need to do is we need to set the game state to the intermission game state. 444 00:30:29,180 --> 00:30:32,780 So as long as the zombies that are left are zero. 445 00:30:33,520 --> 00:30:36,280 And we're still in the wave in progress game state. 446 00:30:36,280 --> 00:30:45,040 So if the workspace get attribute game state is equal to the game state enum of wave in progress. 447 00:30:46,270 --> 00:30:52,330 Then we can go ahead and set the workspace to the intermission. 448 00:30:53,030 --> 00:30:59,570 State, but we only want to do this if the current wave that we're at is not the last wave. 449 00:30:59,750 --> 00:31:06,620 So if the current wave that we're at is not equal to the last wave, which would be the 11th wave, 450 00:31:06,620 --> 00:31:09,980 then we can go ahead and set it to the intermission state. 451 00:31:10,010 --> 00:31:14,570 Otherwise, this means that our players have beat the game. 452 00:31:14,570 --> 00:31:19,970 So players beat the game because they are at the last wave and all of the zombies have died. 453 00:31:20,000 --> 00:31:23,570 Then this is where we would set the game state to the game state. 454 00:31:23,600 --> 00:31:25,490 Enum of game over. 455 00:31:25,490 --> 00:31:26,180 Win. 456 00:31:26,920 --> 00:31:33,520 And like I said, we want to continually spawn these zombies on the map until the number of zombies 457 00:31:33,520 --> 00:31:35,830 we have left to spawn in this table is zero. 458 00:31:35,830 --> 00:31:43,810 So if the number of elements in our zombies to spawn table is equal to zero or less than or equal to 459 00:31:43,810 --> 00:31:45,070 zero, we'll just do that. 460 00:31:45,700 --> 00:31:51,520 Or we also want to stop spawning zombies in the workspace when, for example, all of the players die 461 00:31:51,520 --> 00:31:52,300 in our game. 462 00:31:52,480 --> 00:31:56,020 So let's say we're repeatedly spawning zombies on the map. 463 00:31:56,020 --> 00:31:59,680 But while we are spawning zombies on the map, all of the players have died. 464 00:31:59,680 --> 00:32:02,410 Then we want to also stop spawning zombies. 465 00:32:02,410 --> 00:32:06,520 So we'll do workspace get attribute game state. 466 00:32:06,670 --> 00:32:13,510 If the game state is no longer equal to the game state enum of wave in progress, then we don't want 467 00:32:13,510 --> 00:32:15,040 to spawn any more zombies. 468 00:32:16,020 --> 00:32:21,600 And then, in order to keep track of how many zombies are left in our zombies to spawn table after we 469 00:32:21,600 --> 00:32:26,430 have spawned the zombie onto the map and we're listening to their diet event, we can go ahead and remove 470 00:32:26,430 --> 00:32:28,620 them from our zombies to spawn table. 471 00:32:28,620 --> 00:32:36,030 So table dot remove zombies to spawn at this current random number index. 472 00:32:36,480 --> 00:32:39,870 And then a very important thing we need to do is add a yield statement in here. 473 00:32:39,870 --> 00:32:44,010 Because I don't want all of my zombies to spawn instantaneously. 474 00:32:44,010 --> 00:32:49,500 So we'll put a yield statement in here of let's say 0.25 seconds. 475 00:32:50,290 --> 00:32:56,260 Okay, so now this means once the start intermission is over and we get put into the wave in progress 476 00:32:56,260 --> 00:33:01,720 state, this function is going to execute, and then it's going to go through and spawn all of these 477 00:33:01,720 --> 00:33:04,300 zombies for a particular wave onto the map. 478 00:33:04,390 --> 00:33:08,410 And it's going to listen for when all of those zombies are dead. 479 00:33:08,930 --> 00:33:15,800 And when all the zombies are dead, if we're at the last wave, we're going to set the game to be over 480 00:33:15,800 --> 00:33:16,790 and the players have won. 481 00:33:16,790 --> 00:33:19,100 Otherwise we're going to set it back to the intermission. 482 00:33:19,100 --> 00:33:23,420 So let's go ahead and test what we've gotten written down so far. 483 00:33:24,420 --> 00:33:27,720 Currently we are in the starting intermission and if I play. 484 00:33:28,330 --> 00:33:31,750 We now have to wait through this set of 30s. 485 00:33:33,320 --> 00:33:35,870 So I'll go ahead and wait for that to be over. 486 00:33:37,610 --> 00:33:39,920 321. 487 00:33:39,920 --> 00:33:41,720 It should tell me how many zombies. 488 00:33:41,720 --> 00:33:42,260 There we go. 489 00:33:42,260 --> 00:33:43,820 Wave one in progress. 490 00:33:43,820 --> 00:33:46,040 12 zombies left. 491 00:33:46,370 --> 00:33:51,320 And as you can see, the zombies have been placed in their particular spawn areas. 492 00:33:51,320 --> 00:33:54,980 So if I go walk over here, they should start chasing after me. 493 00:33:54,980 --> 00:33:58,250 As you can see, there's three zombies in that spawn. 494 00:33:58,460 --> 00:34:03,260 And then if I run down here, there should be another set of three zombies over here. 495 00:34:03,380 --> 00:34:03,620 Okay. 496 00:34:03,620 --> 00:34:04,880 There's one. 497 00:34:05,300 --> 00:34:06,530 Only one zombie. 498 00:34:07,010 --> 00:34:08,600 There should be more in here. 499 00:34:12,940 --> 00:34:15,430 So far, we're being chased by six zombies. 500 00:34:15,430 --> 00:34:16,240 Oh, I guess they did. 501 00:34:16,270 --> 00:34:17,440 I wonder where they came from. 502 00:34:17,440 --> 00:34:19,750 And then here's another six zombies. 503 00:34:21,670 --> 00:34:26,020 And then where is my final six or my final three? 504 00:34:26,020 --> 00:34:27,820 My final three zombies are over here as well. 505 00:34:27,820 --> 00:34:28,600 Okay, perfect. 506 00:34:28,600 --> 00:34:32,800 They all spawned in their areas and now I'm being chased by 12 zombies. 507 00:34:32,800 --> 00:34:38,860 So now what we need to do is we first need to give our player a weapon to defend themselves on this 508 00:34:38,860 --> 00:34:42,700 first wave, because clearly they're not going to have any money to buy any weapons. 509 00:34:43,270 --> 00:34:48,970 So we need to give all the players, like the starting knife to defend themselves against the zombies. 510 00:34:48,970 --> 00:34:52,300 And when our zombies die from these guys. 511 00:34:52,300 --> 00:34:53,710 So let me go ahead and die. 512 00:34:54,670 --> 00:34:59,470 When all the players in our game die, we need to go ahead and display the message that all of our players 513 00:34:59,470 --> 00:35:05,350 have lost, and we need to go ahead and clean up all these zombies off of our map. 514 00:35:06,740 --> 00:35:10,160 So to tackle this issue back inside of our game handler. 515 00:35:10,710 --> 00:35:16,740 When the respawn event is fired for a particular player, and then we place them on the map, we need 516 00:35:16,740 --> 00:35:21,570 to go ahead and give them a knife or a starting knife to defend themselves with. 517 00:35:21,600 --> 00:35:27,420 So inside of server storage, we're going to get the knife tool and we're going to make a clone of that. 518 00:35:27,570 --> 00:35:29,100 So we'll get that clone. 519 00:35:29,100 --> 00:35:31,140 And we want to go ahead and give that to the player. 520 00:35:31,140 --> 00:35:34,440 So we'll set the parent equal to that player's backpack. 521 00:35:34,440 --> 00:35:40,200 So now all the players that spawn into our game will have this knife to defend themselves. 522 00:35:40,750 --> 00:35:45,580 And then we need to listen for when there is no players left in our live team. 523 00:35:45,610 --> 00:35:51,280 So inside of our teams and we get the live team, we want to listen to the player removed event and 524 00:35:51,280 --> 00:35:52,600 connect a function. 525 00:35:52,780 --> 00:35:58,540 And here we want to check if the number of alive players is equal to zero. 526 00:35:58,540 --> 00:36:02,980 So we'll get the number of alive players, or we'll get all the alive players. 527 00:36:02,980 --> 00:36:04,720 That's going to be equal to teams. 528 00:36:04,720 --> 00:36:07,030 Dot alive get players. 529 00:36:07,990 --> 00:36:11,410 And if the number of alive players is equal to zero. 530 00:36:12,120 --> 00:36:20,940 Then that means we should set the workspace attribute of game state equal to the game state enum of 531 00:36:20,940 --> 00:36:21,960 game over. 532 00:36:21,960 --> 00:36:30,990 But of course we only want to do this if the workspace is game state is not equal to the game over game 533 00:36:30,990 --> 00:36:33,000 state or the waiting game state. 534 00:36:33,000 --> 00:36:37,770 So for example, if the workspace get attribute game state. 535 00:36:38,940 --> 00:36:45,600 If that is equal to the game state of let's say we're already in the game over game state, then I want 536 00:36:45,600 --> 00:36:49,170 to ignore this code down here. 537 00:36:49,170 --> 00:36:51,090 So this is where we would just return. 538 00:36:51,390 --> 00:36:55,710 Another thing I want to ignore is if we're in the game over win game state. 539 00:36:55,710 --> 00:36:58,440 So let me copy this paste this here. 540 00:36:58,440 --> 00:37:04,770 If we're in the game over win then I also want to ignore this code down here. 541 00:37:05,490 --> 00:37:08,970 Or let's say we are in the waiting game state. 542 00:37:08,970 --> 00:37:11,190 Then I also want to ignore this code down here. 543 00:37:12,400 --> 00:37:19,660 So if we're in the game state of waiting, then we're also going to ignore this code. 544 00:37:19,810 --> 00:37:23,920 So we're only going to set the attribute on the workspace to the game over game state. 545 00:37:23,920 --> 00:37:27,550 If we're in the intermission or the wave in progress game state. 546 00:37:28,600 --> 00:37:31,270 So now this means if I go and playtest my game. 547 00:37:33,400 --> 00:37:34,990 And I jump in to play. 548 00:37:35,820 --> 00:37:39,660 As you can see, I got my knife from the server to defend myself. 549 00:37:39,870 --> 00:37:43,440 And now we're just going to wait for this countdown to complete. 550 00:37:43,620 --> 00:37:46,890 And we're going to have all these zombies come after us. 551 00:37:47,970 --> 00:37:50,220 And then here I have all these zombies coming after me. 552 00:37:50,220 --> 00:37:54,300 If I go ahead and start killing these guys, let me just clean these guys off the map. 553 00:37:58,010 --> 00:37:58,340 Okay. 554 00:37:58,340 --> 00:38:00,440 Got that set killed off. 555 00:38:00,440 --> 00:38:02,600 Let's go ahead and go to the zombies over here. 556 00:38:02,600 --> 00:38:04,700 There should be three zombies over here. 557 00:38:05,120 --> 00:38:07,520 So there's my first, second and third. 558 00:38:07,520 --> 00:38:09,260 Let's go ahead and kill these guys. 559 00:38:15,050 --> 00:38:15,320 Are. 560 00:38:15,320 --> 00:38:16,700 Those guys are dead? 561 00:38:16,700 --> 00:38:18,200 Let's run over here. 562 00:38:19,770 --> 00:38:21,570 Let's go ahead and kill these guys. 563 00:38:21,960 --> 00:38:22,980 That guy's dead. 564 00:38:24,240 --> 00:38:25,200 Dead? 565 00:38:25,800 --> 00:38:26,700 And they're dead. 566 00:38:26,700 --> 00:38:29,970 And as you can see, it's updating the text on our screen when those zombies die. 567 00:38:30,000 --> 00:38:32,970 So now it's telling us we have three zombies left. 568 00:38:32,970 --> 00:38:34,020 Very cool. 569 00:38:34,380 --> 00:38:36,420 Let's go ahead and kill these last guys. 570 00:38:37,680 --> 00:38:41,910 And after all, these guys are dead, it should set us back to the intermission state. 571 00:38:41,910 --> 00:38:43,950 So let's go ahead and see if it does that. 572 00:38:45,270 --> 00:38:46,800 So all these zombies are dead. 573 00:38:46,800 --> 00:38:48,840 And as you can see, it printed in the console. 574 00:38:48,840 --> 00:38:49,950 Intermission. 575 00:38:50,400 --> 00:38:52,470 So right now we're in the intermission. 576 00:38:53,640 --> 00:38:57,750 And it should set us back to the next wave. 577 00:38:58,830 --> 00:39:01,050 Or actually, I believe. 578 00:39:01,050 --> 00:39:01,650 Yeah. 579 00:39:02,140 --> 00:39:04,720 So we haven't done anything in our intermission function. 580 00:39:04,720 --> 00:39:07,420 So we actually need to go ahead and fill this out. 581 00:39:07,450 --> 00:39:10,870 So it did set us to the intermission state. 582 00:39:10,870 --> 00:39:16,570 So now we need to script what exactly we would like to happen when we are in the intermission state. 583 00:39:17,080 --> 00:39:21,130 Well, what we want to go ahead and do is basically the same thing that we've done up here. 584 00:39:21,910 --> 00:39:27,790 So we'll just copy this code and paste it in here. 585 00:39:28,210 --> 00:39:34,330 And we want to wait for the total amount of time for the regular intermission, which is 60s. 586 00:39:35,230 --> 00:39:41,290 And then we'll change this value here to be for the regular intermission as well as right here. 587 00:39:42,140 --> 00:39:47,450 And then we're just going to display that we are currently in an intermission. 588 00:39:48,610 --> 00:39:55,360 And that we have however many seconds until next wave. 589 00:39:57,400 --> 00:40:02,350 So once we kill all of these zombies and we get placed into the intermission state, it's going to do 590 00:40:02,350 --> 00:40:04,060 a countdown of 60s. 591 00:40:04,060 --> 00:40:10,120 And then once that countdown is up, it's going to allow us to move to the next wave. 592 00:40:11,520 --> 00:40:13,680 Okay, so I got three zombies left. 593 00:40:13,680 --> 00:40:17,640 After I kill these three guys, it should set us to the intermission state. 594 00:40:17,640 --> 00:40:20,910 And then we should see a countdown appear on our screen. 595 00:40:20,910 --> 00:40:23,010 So let's go ahead and kill these guys. 596 00:40:23,790 --> 00:40:25,110 Yeah, perfect. 597 00:40:25,110 --> 00:40:29,460 It says intermission, however many seconds until the next wave. 598 00:40:29,460 --> 00:40:36,360 So when this intermission is up, it should start the next wave and spawn the next set of zombies. 599 00:40:36,360 --> 00:40:41,040 So let's go ahead and wait for this intermission to end, and we'll see if it starts the next wave. 600 00:40:42,130 --> 00:40:47,170 We got five, four, three, two, one. 601 00:40:47,170 --> 00:40:47,980 Perfect. 602 00:40:47,980 --> 00:40:49,660 We now are in wave two. 603 00:40:49,690 --> 00:40:51,610 So wave two in progress. 604 00:40:51,610 --> 00:40:53,890 And now we have 20 zombies left. 605 00:40:53,890 --> 00:40:55,660 We need to fight. 606 00:40:55,660 --> 00:40:59,710 And you can also see that I have gotten some money from killing these zombies. 607 00:40:59,710 --> 00:41:03,130 So every time I kill my zombie, I get some money. 608 00:41:03,130 --> 00:41:10,630 Another thing we could implement later in a future lecture is allowing us to earn some extra money when 609 00:41:10,630 --> 00:41:11,590 a wave ends. 610 00:41:11,590 --> 00:41:17,770 So at the end of each wave, we could have a reward for our players to have some extra bit of money. 611 00:41:20,200 --> 00:41:23,650 But for now, let's just say the zombies kill me and I die. 612 00:41:23,650 --> 00:41:28,510 It should set the game state to the game over and we lost. 613 00:41:28,510 --> 00:41:30,250 So let's go ahead and see. 614 00:41:30,460 --> 00:41:31,390 There we go. 615 00:41:31,390 --> 00:41:32,680 We have died. 616 00:41:32,680 --> 00:41:36,310 And as you can see in the console it printed game over lose. 617 00:41:36,310 --> 00:41:37,780 So we've lost. 618 00:41:37,780 --> 00:41:43,090 So in that state we could have a message print out saying like all the players lost. 619 00:41:43,090 --> 00:41:47,680 And then it should set us to the clean up state or the waiting state. 620 00:41:48,240 --> 00:41:53,160 Too many print statements coming from my zombies, so it's kind of flooding my console. 621 00:41:53,760 --> 00:42:01,770 Actually, a neat little tip I want to show you guys is if we don't want to have the zombies print this 622 00:42:01,770 --> 00:42:06,660 garbage out in our console anymore, and you want to be able to quickly remove it without having to 623 00:42:06,660 --> 00:42:09,930 go into each one of the individual scripts for your zombie. 624 00:42:09,930 --> 00:42:16,470 I well, you can go ahead and do is you can press control, shift and F on your keyboard to open up 625 00:42:16,470 --> 00:42:18,420 this little menu here. 626 00:42:19,150 --> 00:42:20,740 And it's also in the view tab. 627 00:42:20,740 --> 00:42:26,680 It's called find All, replace All, and we can go ahead and search for our print statement when we're 628 00:42:26,680 --> 00:42:29,110 printing wandering into the console. 629 00:42:29,110 --> 00:42:34,690 So if I search for that boom, it shows me all the results from all of these scripts where this exists. 630 00:42:34,690 --> 00:42:37,090 And then I can replace this with a value. 631 00:42:37,090 --> 00:42:41,680 So if you push down this little drop down arrow, you can give a value you would like to replace it 632 00:42:41,680 --> 00:42:42,160 with. 633 00:42:42,160 --> 00:42:46,900 And in this case I just want to delete all of them from my zombie AI scripts. 634 00:42:46,900 --> 00:42:49,480 So we can just hit replace all hit. 635 00:42:49,480 --> 00:42:50,170 Yes. 636 00:42:50,170 --> 00:42:55,480 And now it has deleted that print statement from our zombies for us, which is very cool. 637 00:42:55,690 --> 00:43:01,300 This is just a little extra neat feature in Roblox studio to allow you to replace or find things in 638 00:43:01,300 --> 00:43:02,650 scripts very quickly. 639 00:43:04,030 --> 00:43:12,670 However, now what we want to do is when the game state is set to the game over, we lost. 640 00:43:12,850 --> 00:43:18,460 Then we should print a message or give a message to all of our players saying, hey, you guys lost 641 00:43:18,460 --> 00:43:24,550 and then we'll wait for a few seconds and then set the game to this waiting state, and this function 642 00:43:24,550 --> 00:43:26,260 will clean up the game for us. 643 00:43:26,260 --> 00:43:33,160 So for example, here, what we need to do is I'm going to copy this up here. 644 00:43:33,750 --> 00:43:34,920 And then place it here. 645 00:43:34,920 --> 00:43:39,510 But instead, the message we're going to give to our players is that we lost. 646 00:43:39,510 --> 00:43:42,270 So we'll say game over. 647 00:43:44,560 --> 00:43:46,720 And we'll say zombies win. 648 00:43:48,230 --> 00:43:51,560 And then we'll wait for a random amount of time like 10s. 649 00:43:52,840 --> 00:44:01,540 And then we're going to set the game state on the workspace to be equal to the game state enum of waiting. 650 00:44:01,540 --> 00:44:05,440 And then this function will go ahead and clean up the game for us. 651 00:44:05,440 --> 00:44:12,670 So what we want to do in this particular instance is we want to tell all the players that we are cleaning 652 00:44:12,670 --> 00:44:13,720 up the game. 653 00:44:17,060 --> 00:44:20,720 And then we want to delete all of the zombies that are currently on the map. 654 00:44:20,720 --> 00:44:26,360 So we'll refer to our zombies workspace folder, and we're going to clear all of the children inside 655 00:44:26,360 --> 00:44:28,880 of there, which will delete all of those zombies. 656 00:44:28,880 --> 00:44:34,910 And then another thing I want to do actually is in the workspace filtering folder for the bullet holes, 657 00:44:34,910 --> 00:44:39,230 just in case any players were shooting up the map and there were bullet holes all over the place, I 658 00:44:39,230 --> 00:44:42,500 can go ahead and clear all of those real quick as well. 659 00:44:43,180 --> 00:44:47,350 And then we want to reset the current wave back to zero. 660 00:44:48,030 --> 00:44:54,390 And then another thing we want to do is when we are cleaning up everything, and then we want to start 661 00:44:54,390 --> 00:44:55,410 a new set of waves. 662 00:44:55,410 --> 00:44:59,430 We want to reset the money for all of our players back to zero. 663 00:44:59,430 --> 00:45:06,810 So for every single player in our game and players jet players, we're going to refer to their leader 664 00:45:06,810 --> 00:45:07,650 stats. 665 00:45:09,170 --> 00:45:12,470 And we're going to set their money value equal to zero. 666 00:45:14,140 --> 00:45:18,940 And then we also want to go ahead and kill any players that are still on the map. 667 00:45:18,940 --> 00:45:25,810 So if we won the game, for example, because this state is going to be, um, enabled when we either 668 00:45:25,810 --> 00:45:27,550 lost the game or we've won the game. 669 00:45:27,550 --> 00:45:32,110 So just in case there are still players on the alive team, we're going to loop through every single 670 00:45:32,110 --> 00:45:36,430 alive player in Teams Alive get players. 671 00:45:37,840 --> 00:45:42,730 And we're going to refer to this live player, get their character, get their humanoid, and change 672 00:45:42,730 --> 00:45:46,810 the state of this humanoid to the enum humanoid state type of dead. 673 00:45:46,810 --> 00:45:49,120 So that will kill all of the players in our game. 674 00:45:50,110 --> 00:45:53,230 And then we're going to put a yield statement here of like five seconds. 675 00:45:53,230 --> 00:45:58,360 That way this cleaning up game message stays on the screen for about five seconds. 676 00:45:58,360 --> 00:46:04,630 And then after that, we can have another countdown here to say that we're going to be starting a new 677 00:46:04,630 --> 00:46:09,280 game in however many seconds, so we can have a small countdown of like 10s. 678 00:46:09,890 --> 00:46:11,720 So we'll put our for loop here. 679 00:46:11,720 --> 00:46:18,650 And we're just going to copy this, paste this here and say that we're going to be starting a new game. 680 00:46:19,400 --> 00:46:22,940 N and then we'll concatenate this with our current index. 681 00:46:22,940 --> 00:46:25,700 And then we'll say we're starting a new game and however many seconds. 682 00:46:25,700 --> 00:46:28,250 And then we'll make sure to yield for one second each time. 683 00:46:29,150 --> 00:46:34,460 And then we can go ahead and set the attribute on the workspace to the start intermission. 684 00:46:35,320 --> 00:46:38,230 So now let's go ahead and test to see if this is working. 685 00:46:38,230 --> 00:46:39,580 So we'll play our game. 686 00:46:41,130 --> 00:46:47,190 And I'm waiting for all my zombies to spawn in and let's have my zombies kill me. 687 00:46:47,190 --> 00:46:49,110 So if I walk over here. 688 00:46:49,870 --> 00:46:52,870 Let's have these guys smack me to death. 689 00:46:53,770 --> 00:46:57,160 And then when I die, it should hopefully clean up the game for us. 690 00:46:57,160 --> 00:46:58,900 So these guys are hurting me. 691 00:46:59,230 --> 00:47:00,100 They're hurting me. 692 00:47:00,130 --> 00:47:00,910 Always. 693 00:47:00,910 --> 00:47:05,020 I'm getting hurt real bad, and eventually they should kill me. 694 00:47:05,170 --> 00:47:06,280 Okay, there we go. 695 00:47:06,310 --> 00:47:06,910 I'm dead. 696 00:47:06,940 --> 00:47:07,450 There we go. 697 00:47:07,450 --> 00:47:08,470 It says game over. 698 00:47:08,470 --> 00:47:09,610 Zombies win. 699 00:47:10,450 --> 00:47:14,830 And then it deleted all the zombies off the map as well. 700 00:47:14,830 --> 00:47:15,580 Hopefully. 701 00:47:15,580 --> 00:47:16,300 No, they're still there. 702 00:47:16,300 --> 00:47:16,870 Okay, there we go. 703 00:47:16,870 --> 00:47:17,800 Now they're all cleaned up. 704 00:47:17,800 --> 00:47:23,650 It says cleaning up game deleted all of these zombies, and now it says starting a new game in however 705 00:47:23,650 --> 00:47:24,370 many seconds. 706 00:47:24,370 --> 00:47:29,200 And if I try to press the play button, I can't spawn into the map because we're not in the starting 707 00:47:29,200 --> 00:47:29,860 intermission. 708 00:47:29,860 --> 00:47:34,450 But as you can see, it has set the game back to the starting intermission and it says waiting for players. 709 00:47:34,450 --> 00:47:37,330 And if I play the game, boom! 710 00:47:37,330 --> 00:47:41,410 Now we're back in the starting intermission and it's starting the countdown again, and we're going 711 00:47:41,410 --> 00:47:43,090 to go back to the first wave. 712 00:47:43,090 --> 00:47:44,710 So that's working great. 713 00:47:44,800 --> 00:47:48,490 Now let's say I were to reset my character in the middle of this countdown. 714 00:47:48,490 --> 00:47:51,340 So if I die, game over. 715 00:47:51,340 --> 00:47:57,370 Zombies win because I was the only player on the alive team, and now we have to wait for the game to 716 00:47:57,370 --> 00:47:58,390 clean up the map again. 717 00:47:58,390 --> 00:47:59,200 Perfect. 718 00:48:00,580 --> 00:48:04,210 The last thing that we can go ahead and fill out is when we win the game. 719 00:48:04,210 --> 00:48:13,630 So we'll just copy this and we'll just paste this in here and say that the game is over, but you win 720 00:48:13,630 --> 00:48:14,440 just like that. 721 00:48:14,440 --> 00:48:18,430 And then we'll wait for 10s and then put the game in the cleanup state. 722 00:48:18,550 --> 00:48:19,420 Okay. 723 00:48:19,420 --> 00:48:25,690 And the next lecture, we're going to continue improving upon our wave system by adding sound effects, 724 00:48:25,720 --> 00:48:28,570 giving money to players at the end of a wave. 725 00:48:28,570 --> 00:48:34,270 We're also going to highlight the last zombie on the map, so that way it's easier for the players to 726 00:48:34,270 --> 00:48:37,390 see where exactly the last zombie is and stuff like that. 727 00:48:37,390 --> 00:48:40,330 So I will see you in the next lecture.